package com.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.dto.TextTool.TextTool;
import com.example.entity.vo.request.text.CountRequestVO;
import com.example.entity.vo.request.text.LetterChangeRequestVO;
import com.example.entity.vo.response.text.CountResponseVO;
import com.example.entity.vo.response.text.LetterChangeResponseVO;
import com.example.mapper.TextToolMapper;
import com.example.service.TextToolService;
import org.springframework.stereotype.Service;

/**
 * 文本工具服务实现类
 *
 * @auth shanyue
 * @date 2025年07月09日14:08
 */
@Service
public class TextToolServiceImpl extends ServiceImpl<TextToolMapper, TextTool> implements TextToolService {

    /**
     * 根据路径查询对应的文本工具信息
     *
     * @param path 工具路径标识
     * @return 查询到的TextTool对象，不存在返回null
     */
    @Override
    public TextTool getInfoByPath(String path) {
        return lambdaQuery()
                .eq(TextTool::getPath, path)
                .one();
    }

    /**
     * 统计文本信息
     *
     * @param requestVO 请求体，包含用户输入文本
     * @return 统计结果的响应大雨下CountResponseVO
     */
    @Override
    public CountResponseVO getCountResult(CountRequestVO requestVO) {
        String text = requestVO.getText();
        if (text == null || text.isEmpty()) {
            text = "";
        }

        //总字数(按Unicode codePoint计算，兼容表情)
        int totalCount = text.codePointCount(0, text.length());

        //字符数(char长度，包含不可见字符)
        int characterCount = text.length();

        //行数(\r\n   \r  \n)
        int lineCount = text.isEmpty() ? 0 : text.split("\r\n|\r|\n").length;

        //数字(0-9)
        long numberCount = text.chars().filter(Character::isDigit).count();

        //字母(a-zA-Z)
        long letterCount = text.chars()
                .filter(ch -> (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
                .count();

        return new CountResponseVO(
                String.valueOf(totalCount),
                String.valueOf(characterCount),
                String.valueOf(lineCount),
                String.valueOf(numberCount),
                String.valueOf(letterCount)
        );
    }

    /**
     * 将文本进行大小写转换
     *
     * @param requestVO 请求体，封装需要转换的文本内容以及类型(upper或lower)
     * @return 转换后的文本结果封装在LetterChangeResponseVO仲
     */
    @Override
    public LetterChangeResponseVO convertText(LetterChangeRequestVO requestVO) {
        String text = requestVO.getText();
        String type = requestVO.getType();
        LetterChangeResponseVO result = new LetterChangeResponseVO(text);

        if (text == null || text.isEmpty() || type == null || type.isEmpty()) {
            //文本为空或转换类型不明确，返回原文本
            return result;
        }

        boolean toUpper = "upper".equalsIgnoreCase(type);
        StringBuilder stringBuilder = new StringBuilder(text.length());

        //逐字符转换大小写，非字母字符保持不变
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                stringBuilder.append(toUpper ? Character.toUpperCase(c) : Character.toLowerCase(c));
            } else {
                stringBuilder.append(c);
            }
        }
        result.setResult(stringBuilder.toString());
        return result;

    }


}
